package com.stlm2.controller;

import com.stlm2.controller.pay.WechatConfig;
import com.stlm2.core.memcached.MemcachedClient;
import com.stlm2.util.HttpSend;
import com.stlm2.util.Memcached;
import com.stlm2.util.codecontent.CodeContent;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Author：fuchunyu
 * Date：2017/9/13
 * Description：
 */
@Controller
@RequestMapping("/shareC")
public class ShareController {
    @Resource
    private Memcached memcached;

    @ResponseBody
    @RequestMapping("/getAccessToken.json")
    public String getAccessToken(String companyCode){
        Object ticket=memcached.get("ticket");
        if(ticket==null){
        WechatConfig wechatConfig=new WechatConfig();
        String url="https://api.weixin.qq.com/cgi-bin/token";
        String param="grant_type=client_credential&appid="+wechatConfig.getAppID()+"&secret="+wechatConfig.getAppSecret();
        String result=HttpSend.getSend(url,param);
        JSONObject jsonObject=new JSONObject(result);
        String token=jsonObject.get("access_token").toString();
        System.out.println(result);

        String ticketUrl="https://api.weixin.qq.com/cgi-bin/ticket/getticket";
        String ticketParam="access_token="+token+"&type=jsapi";
        String ticketResult=HttpSend.getSend(ticketUrl,ticketParam);
        JSONObject json=new JSONObject(ticketResult);
        ticket=json.get("ticket");
        memcached.set("ticket",7200,ticket);
        }
        return CodeContent.returnSuccess(ticket.toString());
    }
}
